package com.taru.university.controller;


import com.taru.university.model.User;
import com.taru.university.service.UserService;
import com.taru.university.utils.Constant;
import com.taru.university.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Controller
@CrossOrigin
public class UserController {
    private static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    UserService userService;

    @RequestMapping("/query/user/id")
    @ResponseBody
    public Object queryUserById(String userId){
        JsonResult result = null;
        try {
            User user= userService.queryUserById(userId);
            logger.info("用户Id：{}", userId);
            result = new JsonResult("查询成功", Constant.STATUC_SUCCESS, user);
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("查询异常", Constant.STATUC_FAILURE);
        }
        return result;
    }

    @RequestMapping("/login")
    @ResponseBody
    public Object login(String userName, String userPassword, String userSort){
        JsonResult result = null;
        try {

            User user = userService.login(userName, userPassword,userSort);
            logger.info("用户名：{} 用户密码：{}用户类型：{}", userName, userPassword,userSort);
            if(user != null){
                result = new JsonResult("登录成功", Constant.STATUC_SUCCESS, user);
            }else{
                result = new JsonResult("账户或密码错误", Constant.STATUC_FAILURE);
            }

        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("登录异常", Constant.STATUC_FAILURE);
        }
        return result;
    }

    @RequestMapping("/update/user")
    @ResponseBody
    public Object updateUserByQuestion(User users){
        JsonResult result = null;
        try {
            userService.updateUserByQuestion(users);
            logger.info("用户信息", users);
            result = new JsonResult("更新成功", Constant.STATUC_SUCCESS);
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("更新异常", Constant.STATUC_FAILURE);
        }
        return result;
    }

    @RequestMapping("/update/user/password")
    @ResponseBody
    public Object updateUserById(User users){
        JsonResult result = null;
        try {
            userService.updateUserById(users);
            logger.info("用户信息", users);
            result = new JsonResult("更新成功", Constant.STATUC_SUCCESS);
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("更新异常", Constant.STATUC_FAILURE);
        }
        return result;
    }

    @RequestMapping("/insert/user")
    @ResponseBody
    public Object insertUser(User users){
        JsonResult result = null;
        try {
            userService.insertUser(users);
            logger.info("用户信息", users);
            result = new JsonResult("注册成功", Constant.STATUC_SUCCESS, users);
        }catch (Exception ex){
            logger.info(ex.getMessage());
            result = new JsonResult("注册异常", Constant.STATUC_FAILURE);
        }
        return result;
    }
}
